package com.xplay.xpocker.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xplay.xpocker.entity.GameHiMahjongAction;
import com.xplay.xpocker.entity.GameHiRoom;
import com.xplay.xpocker.entity.GameHiRoomDTO;
import com.xplay.xpocker.entity.GameHiRoomUser;
import com.xplay.xpocker.mapper.GameHiRoomMapper;
import com.xplay.xpocker.service.GameHiMahjongActionService;
import com.xplay.xpocker.service.GameHiRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xplay.xpocker.service.GameHiRoomUserService;
import com.xplay.xpocker.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xplay
 * @since 2024-05-12
 */
@Service
public class GameHiRoomServiceImpl extends ServiceImpl<GameHiRoomMapper, GameHiRoom> implements GameHiRoomService {
    @Autowired
    private GameHiRoomUserService hiRoomUserService;
    @Resource
    private GameHiRoomMapper hiRoomMapper;

    @Resource
    private GameHiMahjongActionService actionService;

    @Override
    public void addHistory(GameHiRoomDTO hiRoomDTO) {
        hiRoomDTO.setId(null);
        save(hiRoomDTO);
        if (ObjectUtils.notEmpty(hiRoomDTO.getAllUsers())) {
            for (GameHiRoomUser user : hiRoomDTO.getAllUsers()) {
                user.setId(null);
                user.setHiRoomId(hiRoomDTO.getId());
            }
            hiRoomUserService.saveBatch(hiRoomDTO.getAllUsers());
        }
    }

    @Override
    public Page<GameHiRoomDTO> queryUserHistory(Page<GameHiRoomDTO> page, BigDecimal userId) {
        page.setRecords(hiRoomMapper.queryUserHistoryRoomInfo(page, userId));
        return page;
    }

    @Override
    public List<GameHiRoomDTO> queryUserHistoryRoomItemList(BigDecimal roomId) {
        List<GameHiRoomDTO> result = hiRoomMapper.queryUserHistoryRoomItemInfo(roomId);
        return result;
    }

    @Override
    public List<GameHiMahjongAction> recodeHistoryInfo(BigDecimal roomId, Integer severalInning) {
        List<GameHiMahjongAction> list = actionService.lambdaQuery().eq(GameHiMahjongAction::getRoomId, roomId).eq(GameHiMahjongAction::getSeveralInning, severalInning)
                .orderBy(true, true, GameHiMahjongAction::getId).list();
        return list;
    }
}
